package com.huihang.core.utils;


import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;


public class DateUtils {

    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    // 其他方法...

    /**
     * 获得当前日期 yyyy-MM-dd HH:mm:ss
     */
    public String getCurrentTime() {
        return LocalDateTime.now().format(DATE_TIME_FORMATTER);
    }

    /**
     * 获取系统当前时间戳
     */
    public String getSystemTime() {
        return String.valueOf(System.currentTimeMillis());
    }

    /**
     * 获取当前日期 yyyy-MM-dd
     */
    public String getDateByString() {
        return LocalDate.now().format(DATE_FORMATTER);
    }

    /**
     * 得到两个时间差 格式yyyy-MM-dd HH:mm:ss
     */
    public long dateSubtraction(String start, String end) {
        LocalDateTime startDate = LocalDateTime.parse(start, DATE_TIME_FORMATTER);
        LocalDateTime endDate = LocalDateTime.parse(end, DATE_TIME_FORMATTER);
        return Duration.between(startDate, endDate).toMillis();
    }


    /**
     * 判断当前时间是否在[startTime, endTime]区间
     */
    public boolean isEffectiveDate(LocalDate nowTime, String dateSection) {
        String[] times = dateSection.split(",");
        LocalDate startTime = LocalDate.parse(times[0], DATE_FORMATTER);
        LocalDate endTime = LocalDate.parse(times[1], DATE_FORMATTER);

        return !nowTime.isBefore(startTime) && !nowTime.isAfter(endTime);
    }

}